package com.example.demo.test01;

import org.junit.jupiter.api.Test;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class demo02 {


    public List<Person> sayDemo() {
        List<Person> people = new ArrayList<>();
        people.add(new Person("tom1", 8900, 18, "male", "New Yourk"));
        people.add(new Person("Jack", 7000, 20, "woman", "New Yourk"));
        people.add(new Person("Lily", 6900, 21, "male", "Washington"));
        people.add(new Person("anni", 4900, 39, "woman", "New Yourk"));
        people.add(new Person("tom", 7900, 29, "male", "New Yourk"));
        people.add(new Person("tom", 5900, 19, "woman", "New Yourk"));
        return people;
    }



    @Test
    public  void  test01(){
        List<Person> people = sayDemo();
        //求总和
        Long collect = people.stream().collect(Collectors.counting());
        //求平均工资
        System.out.println("collect = " + collect);
        //求最高工资
        Optional<Integer> collect1 = people.stream().map(Person::getSalary).collect(Collectors.maxBy(Integer::compare));
        System.out.println("collect1 = " + collect1);
        //求工资之和
        Integer sum = people.stream().collect(Collectors.summingInt(Person::getSalary));
        System.out.println("sum = " + sum);

        //一次性统计所有的信息
        DoubleSummaryStatistics collect2 = people.stream().collect(Collectors.summarizingDouble(Person::getSalary));
        System.out.println("collect2 = " + collect2);

    }

    @Test
    public  void   test02(){
        List<Person> people = sayDemo();
        Map<String, Person> collect = people.stream().filter(p -> p.getSalary() > 8000).collect(Collectors.toMap(Person::getName, p -> p));
        System.out.println("collect = " + collect);
    }


    @Test
    public  void  test03(){
        List<Integer> list = Arrays.asList(1, 3, 4, 5, 6, 77, 12);
        //求和的方式1
        Optional<Integer> sum = list.stream().reduce((x, y) -> x + y);
        System.out.println("sum = " + sum);

        //求和的方式二
        Optional<Integer> reduce = list.stream().reduce(Integer::sum);
        System.out.println("reduce = " + reduce);

        //求和的方式3
        Integer reduce1 = list.stream().reduce(0, Integer::sum);
        System.out.println("reduce1 = " + reduce1);

        //求乘积
        Optional<Integer> reduce2 = list.stream().reduce((x, y) -> x * y);
        System.out.println("reduce2 = " + reduce2);

        //求最大值
        Optional<Integer> reduce3 = list.stream().reduce((x, y) -> x > y ? x : y);
        System.out.println("reduce3 = " + reduce3);
        //求最大值的写法2
        Integer reduce4 = list.stream().reduce(1, Integer::max);
        System.out.println("reduce4 = " + reduce4);
    }


    @Test
    public  void  test04(){
        List<String> strings = Arrays.asList("m,k,l,a", "1,3,5,7");
        List<String> collect = strings.stream().flatMap(s -> {
            //将每个元素转换成Stream
            String[] split = s.split(",");
            Stream<String> stream = Arrays.stream(split);
            return stream;
        }).collect(Collectors.toList());

        System.out.println("处理前的集合= " + collect);
        System.out.println("处理后的集合= " + strings);
    }


    @Test
    public  void  test05(){
        String[] strList = {"abcd", "bcdd", "defde", "fTr"};
        List<String> collect = Arrays.stream(strList).map(String::toUpperCase).collect(Collectors.toList());
        System.out.println("collect = " + collect);
    }


    @Test
    public  void  test06(){
        List<Integer> intList = Arrays.asList(1, 3, 4, 6, 7, 11);
        List<Integer> collect = intList.stream().map(x -> x + 3).collect(Collectors.toList());
        System.out.println("collect = " + collect);
        List<Integer> collect1 = collect.stream().map(x -> x + 3).collect(Collectors.toList());
        System.out.println("collect1 = " + collect1);
    }
    
    
    @Test
    public  void  test07(){
        List<Person> people = sayDemo();
        Optional<Person> max = people.stream().max(Comparator.comparingInt(Person::getSalary));
        System.out.println("max.get().getSalary() = " + max.get().getSalary());
    }



    @Test
    public  void   test08(){
        List<String> list = Arrays.asList("adnm", "admmt", "pot", "xbangd", "weoujgsd");
        Optional<String> max = list.stream().max(Comparator.comparing(String::length));
        System.out.println("max = " + max.get());
    }
}






































































































